package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.ISendVerifyCodeService;
import cn.wolfcode.p2p.util.JSONResult;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Controller
public class RegisterController {
        private static final org.slf4j.Logger logger = LoggerFactory.getLogger(RegisterController.class);
        @Autowired
        private ILoginInfoService loginInfoService;
        @Autowired
        private ISendVerifyCodeService sendVerifyCodeService;
        @RequestMapping("list")
        public String list(Model model){
                LoginInfo loginInfo = loginInfoService.selectById(1L);
                logger.info("asdasasdsadasdassadsadasd");
                model.addAttribute("info",loginInfo.getUsername());
                return "page";
        }
        @RequestMapping("userRegister")
        @ResponseBody
        public JSONResult register(String confirmPwd,String password,String username,String verifycode){
                JSONResult jsonResult = new JSONResult();
                try {
                        loginInfoService.save(confirmPwd,password,username,verifycode);
                }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                return jsonResult;
        }

        /**
         * 验证手机是否已经被注册
         * @return
         */
        @RequestMapping("checkPhone")
        @ResponseBody
        public boolean checkPhone(String username){
                return !loginInfoService.checkPhone(username);
        }
        @RequestMapping("sendVerifyCode")
        @ResponseBody
        public JSONResult sendVerifyCode(String phone){
                JSONResult jsonResult = new JSONResult();
                try {
                        sendVerifyCodeService.sendVerifyCode(phone);
                }catch(CustomException e){
                        jsonResult.setMsg(e.getMessage());
                }
                catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg(e.getMessage());
                }

                return jsonResult;
        }

        public static void main(String[] args) {
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                System.out.println(c.get(Calendar.HOUR_OF_DAY));
        }

}
